//
//  PersonalCenterViewController.m
//  TeamInformatization
//
//  Created by bigger on 2019/3/31.
//  Copyright © 2019年 SPIC. All rights reserved.
//

#import "PersonalCenterViewController.h"
#import "PersonalCollectionViewCell.h"
#import "LoginViewController.h"
#import "setListViewController.h"
#import "ClassMeetingViewController.h"
#import "MyTaskViewController.h"
#import "CheckWorkViewController.h"
#import "MyActiveViewController.h"
#import "MyLearnViewController.h"
#import "MyTrainViewController.h"
#import "MyExamViewController.h"
#import "MyCheckViewController.h"
#import "MyInvoicesViewController.h"

//#import "<#header#>"

@interface PersonalCenterViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

// 头像
@property (nonatomic, strong) UIImageView *userLogoView;
// 昵称
@property (nonatomic, strong) SimpleLabel *userNickName;
// 备注
@property (nonatomic, strong) SimpleLabel *remarkLabel;

//@property (nonatomic, strong)

@end

@implementation PersonalCenterViewController

{
    // 8个按钮
    NSArray *_typeImageArr, *_typeTitleArr;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self showTitleBarWithTitle:@"个人中心"];
    // 初始化
    _typeTitleArr = @[@"我的会议", @"我的任务", @"上岗到位", @"我的活动", @"我的学习", @"我的培训", @"我的考试", @"我的审核", @"我的两票"];
    _typeImageArr = @[@"huiyi", @"renwu", @"shanggang", @"huodong", @"xuexi", @"peixun", @"kaoshi", @"shenhe", @"erpiao"];
    
    // UI
    [self loadShowViews];
    
    // Data
    [self requestPersonalCenterData];
}

- (void)requestPersonalCenterData
{
    NSDictionary *params = @{};
    
    [AFNRequest cookPostWithURL:kPersonalMessage params:params success:^(id json) {
        if ([json[@"success"] intValue] == 1) {
            
            [self updateShowViews];
            
        } else {
            
            alertWithTitleMeaage(@"请求失败", json[@"text"]);
        }
        
    } failure:^(NSError *error) {
        
    }];
}

- (void)loadShowViews
{
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = kCommonRedColor;
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kSafeAreaTopHeight);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(140 * kScaleWidth);
    }];
    
    // 头像
    self.userLogoView = [[UIImageView alloc] init];
    [bgView addSubview:_userLogoView];
    [_userLogoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20 * kScaleWidth);
        make.centerY.mas_equalTo(bgView);
        make.size.mas_equalTo(CGSizeMake(80 * kScaleWidth, 80 * kScaleWidth));
    }];
    _userLogoView.layer.masksToBounds = YES;
    _userLogoView.layer.cornerRadius = 40. * kScaleWidth;

    //
    self.userNickName = [SimpleLabel initWithText:@"管理员" TextColor:[UIColor whiteColor] TextFont:[UIFont boldSystemFontOfSize:17] TextAlignment:NSTextAlignmentLeft];
    [bgView addSubview:_userNickName];
    [_userNickName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userLogoView.mas_right).offset(10 * kScaleWidth);
        make.top.mas_equalTo(self.userLogoView.mas_top);
        make.right.mas_equalTo(-10 * kScaleWidth);
    }];

    //
    self.remarkLabel = [SimpleLabel initWithText:@"暂无" TextColor:[UIColor whiteColor] TextFont:[UIFont systemFontOfSize:13] TextAlignment:NSTextAlignmentLeft];
    [bgView addSubview:_remarkLabel];
    [_remarkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(bgView);
        make.left.mas_equalTo(self.userNickName.mas_left);
        make.right.mas_equalTo(self.userNickName.mas_right);
    }];
 
    NSLog(@"111111111");
    //
    UIButton *setInfoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [setInfoButton setImage:[UIImage imageNamed:@"set"] forState:UIControlStateNormal];
    [setInfoButton setImageEdgeInsets:UIEdgeInsetsMake(0, -8, 0, 0)];
    [setInfoButton setTitle:@"个人设置" forState:UIControlStateNormal];
    [setInfoButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    setInfoButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [setInfoButton addTarget:self action:@selector(setPersonalInfoAction:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:setInfoButton];
    [setInfoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userNickName.mas_left);
        make.bottom.mas_equalTo(self.userLogoView.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(80, 22));
    }];


    
    // 流水布局
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];      //创建流水布局
    flowLayout.itemSize = CGSizeMake((kScreenWidth - 10 * kScaleWidth) / 3., (kScreenWidth - 10 * kScaleWidth) / 3.);      //设置每个格子的尺寸
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumLineSpacing = 0;        //设置每一行之间的间距
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.headerReferenceSize = CGSizeMake(0, 0);
    flowLayout.footerReferenceSize = CGSizeMake(0, 0);
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 5 * kScaleWidth, 0, 5 * kScaleWidth);
    
    UICollectionView *controlCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, kSafeAreaTopHeight + 140 * kScaleWidth, kScreenWidth, kScreenWidth) collectionViewLayout:flowLayout];
    controlCollectionView.backgroundColor = kCommonBGGaryColor;
    controlCollectionView.delegate = self;
    controlCollectionView.dataSource = self;
    controlCollectionView.scrollEnabled = NO;
    [controlCollectionView registerClass:[PersonalCollectionViewCell class] forCellWithReuseIdentifier:@"PersonalCell"];
    controlCollectionView.bounces = NO;
    // 横向滑动
    controlCollectionView.showsVerticalScrollIndicator = NO;
    controlCollectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:controlCollectionView];
    
    UIButton *quitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [quitButton setCommonStyleAndShowText:@"退出"];
    [quitButton addTarget:self action:@selector(quitAppAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:quitButton];
    [quitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-5 * kScaleWidth - kSafeAreaBottomHeight);
        make.centerX.mas_equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth - 20 * kScaleWidth, 44 * kScaleWidth));
    }];
}


// 更新显示
- (void)updateShowViews
{
    [_userLogoView sd_setImageWithURL:[NSURL URLWithString:@""]];
    _userNickName.text = @"";
    _remarkLabel.text = @"";
}


#pragma mark - CollectionViewDelegate/DataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _typeTitleArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    PersonalCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PersonalCell" forIndexPath:indexPath];
    
    [cell setShowViewCellWithTypeTitle:_typeTitleArr[indexPath.row] ImageStr:_typeImageArr[indexPath.row]];
    
    return cell;
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0: {
            ClassMeetingViewController *meetVC = [[ClassMeetingViewController alloc] init];
            [self.navigationController pushViewController:meetVC animated:YES];
            break;
        }
        case 1: {
            MyTaskViewController *myTaskVC = [[MyTaskViewController alloc] init];
            [self.navigationController pushViewController:myTaskVC animated:YES];
            break;
        }
        case 2: {
            CheckWorkViewController *checkVC = [[CheckWorkViewController alloc] init];
            [self.navigationController pushViewController:checkVC animated:YES];
            break;
        }
        case 3: {
            MyActiveViewController *activeVC = [[MyActiveViewController alloc] init];
            [self.navigationController pushViewController:activeVC animated:YES];
            break;
        }
        case 4: {
            MyLearnViewController *learnVC = [[MyLearnViewController alloc] init];
            [self.navigationController pushViewController:learnVC animated:YES];
            break;
        }
        case 5: {
            MyTrainViewController *trainVC = [[MyTrainViewController alloc] init];
            [self.navigationController pushViewController:trainVC animated:YES];
            break;
        }
        case 6: {
            MyExamViewController *examVC = [[MyExamViewController alloc] init];
            [self.navigationController pushViewController:examVC animated:YES];
            break;
        }
        case 7: {
            MyCheckViewController *checkVC = [[MyCheckViewController alloc] init];
            [self.navigationController pushViewController:checkVC animated:YES];
            break;
        }
        case 8: {
            MyInvoicesViewController *invoiceVC = [[MyInvoicesViewController alloc] init];
            [self.navigationController pushViewController:invoiceVC animated:YES];
            break;
        }
        default:
            break;
    }
    
    
}
                                                   
// 设置个人信息
- (void)setPersonalInfoAction:(UIButton *)sender
{
    setListViewController *setListVC = [[setListViewController alloc] init];
    [self.navigationController pushViewController:setListVC animated:YES];
}

                                                   
                                                   

// 退出登录
- (void)quitAppAction:(UIButton *)sender
{
    
    [AFNRequest getWithURL:kLogoutApp params:nil success:^(id json) {
        
        if ([json[@"success"] intValue] == 1) {
            
            LoginViewController *vc = [LoginViewController new];
            vc.callBack = ^(BOOL success) {
                [self dismissViewControllerAnimated:YES completion:nil];
            };
            [self presentViewController:vc animated:YES completion:nil];
            
        } else {
            alertWithTitleMeaage(@"退出失败", json[@"text"]);
        }
        
    } failure:^(NSError *error) {
        
    }];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
